import { constantRouterMap } from '@/router/routers'
// import Layout from '@/layout/index'

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
    permission_menus: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, asyncRouter) {
      commit('SET_ROUTERS', asyncRouter)
    }
  }
}

export const filterAsyncRouter = (routers, menus) => { // 遍历后台传来的路由字符串，转换为组件对象
  return routers.filter(router => {
    if (hasPermission(menus, router)) {
      if (router.children && router.children.length) {
        router.children = filterAsyncRouter(router.children, menus)
      }
      return true
    }
    return false
  })
}

// export const loadView = (view) => {
//   return () => import(`@/views/${view}`)
// }

function hasPermission(menus, route) {
  if (route.authority) {
    if (menus[route.authority] !== undefined) {
      return menus[route.authority]
    } else {
      return false
    }
  } else {
    return true
  }
}

export default permission
